<?php
if (!defined('IN_WONDERSHARE'))
{
die('Hacking attempt');
}
require('./classes/module/module_feature.php');
class FeatureControl
{
	var $feature_m;
	function __construct()
	{
	
		$this->feature_m = new FeatureModule();
	}
	/**
	* 批量插入修改特点信息
	*
	* @param   string         $str          字符串
	* @param   string        $product_id    商品id
	* @param   string        $type          操作类型
	* @return  int
	*/
	function  insert_feature_info($str,$product_id,$type)
	{
	
		if($type == 'add')
		{
		
			$sql ="INSERT INTO feature  (product_id  ,feature_title  ,feature_pic_min ,feature_pic_max 
			,feature_intro,feature_type ,feature_order ,add_time , add_user )  VALUES $str ";
		
		}elseif($type == 'update')
		{
			$sql = $str;	
		}
		return $this->feature_m->change_info($sql);
	}

	/**
	* 得到数据提交条件
	*
	* @param   int        $feature_type     特点类型
	* @param   int        $product_id       商品id
	* @param   int        $feature_title    特点标题部分名称
	* @return  string
	*/
	function handle_post($feature_type,$product_id,$feature_title)
	{ 
		$where_str = '';
		if(!empty($feature_type))
		{
			$feature_type = (int)$feature_type == 2?0:(int)$feature_type ;
			$where_str.=' AND feature_type = '.$feature_type;
		}
		if(!empty($product_id))
		{
		
			$where_str.=' AND product_id = '.(int)$product_id;
		}
		if(!empty($feature_title))
		{
			$feature_title = trim($feature_title);
			$where_str.=" AND feature_title LIKE '%".$feature_title."%' ";
		}
		return  $where_str;
	}
	/**
	* 得到数据提交条件返回url参数
	*
	* @param   int        $feature_type     特点类型
	* @param   int        $product_id       商品id
	* @param   int        $feature_title    特点标题部分名称
	* @return  string
	*/
	function get_url_par($feature_type,$product_id,$feature_title)
	{ 
		$where_str = '&url_page=1';
		if(!empty($feature_type))
		{
		
			$where_str.='&feature_type='.(int)$feature_type;
		}
		if(!empty($product_id))
		{
		
			$where_str.='&product_id='.(int)$product_id;
		}
		if(!empty($feature_title))
		{
			$feature_title = trim($feature_title);
			$where_str.='&feature_title='.urlencode($feature_title);
		}
		return  $where_str;
	}

	/**
	* 得到分页相关信息
	*
	* @param   int        $page             页码
	* @param   int        $pagesize         取多少条记录
	* @param   int        $type             为list代表没有条件查询 为list_s代表有条件查询
	* @param   int        $feature_type     特点类型
	* @param   int        $product_id       商品id
	* @param   int        $feature_title    特点标题部分名称
	* @return  array
	*/
	function get_url_info($page,$pagesize,$type,$feature_type = 0 ,$product_id = 0 ,$feature_title = '')
	{
		$url_str = '';
		if($type == 'list')
		{
			$sql = "SELECT COUNT(*) AS total FROM feature WHERE is_delete = 0  ";
		}elseif($type == 'list_s')
		{
			$where_str = $this->handle_post($feature_type,$product_id,$feature_title);
			$sql = "SELECT COUNT(*) AS total FROM feature  WHERE is_delete = 0    $where_str  ";
			$url_str = $this->get_url_par($feature_type,$product_id,$feature_title);
		
		}
		$page_info = get_page_info($sql,$pagesize,$page);
		$page_url = get_page_url($page,$page_info['page_num'],$url_str);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}
	/**
	* 得到列表或者其它的特点信息
	*
	* @param   string     $type             类型（list为列表 不带搜索 而 list_s 在列表 带有搜索,edit代表为编辑，p_feature代表取某个商品的feature八点特点,a_record代表根据主键取记录）
	* @param   int        $start            从那行开始
	* @param   int        $pagesize         取多少条记录
	* @param   int        $pagesize   	     取多少条记录
	* @param   int        $feature_id  	     特点主键
	* @param   int        $feature_type     特点类型
	* @param   int        $feature_title    特点标题部分名称
	* @return  array
	*/
	function  get_feature_info($type,$start = 0,$pagesize = 10,$feature_id = 0,$product_id = 0,$feature_type  = 0,$feature_title = '' )
	{
		if($type == 'list')
		{
			$sql = "SELECT feature_id ,product_id ,feature_title,feature_intro ,feature_pic_min ,feature_pic_max,feature_type,feature_order  
			FROM feature  WHERE is_delete = 0  ORDER BY product_id ASC , feature_order ASC , feature_type ASC 
			LIMIT $start,$pagesize ";
		
		
		}elseif($type == 'list_s')
		{
		
			$where_str = $this->handle_post($feature_type,$product_id,$feature_title);
			$sql = "SELECT feature_id ,product_id ,feature_title,feature_intro ,feature_pic_min ,feature_pic_max,feature_type  
			FROM feature   WHERE is_delete = 0   $where_str  ORDER BY product_id ASC , feature_order ASC , feature_type ASC  LIMIT $start,$pagesize ";
			
		}elseif($type == 'p_feature')
		{
			$sql = "SELECT product_id ,feature_id ,feature_title ,feature_intro ,feature_pic_min ,
			feature_pic_max,feature_order  FROM feature  WHERE is_delete = 0 AND product_id  = $product_id  AND feature_type  = $feature_type  
			ORDER BY feature_order ASC ";
		
		}elseif($type == 'a_record')
		{
			$sql = "SELECT feature_title ,feature_id  ,feature_intro ,feature_pic_min ,
			feature_pic_max,feature_order  FROM feature  WHERE is_delete = 0 AND feature_id  = $feature_id   ";
		
		}
		return $this->feature_m->get_feature_info($sql,$type);
	
	}
	/**
	* ajax改变值
	*
	* @param   int      $ar_id           文章id
	* @param   int      $change_value    值
	* @param   int      $field           要改变的数据字段
	* @return  int
	*/
	function  change_info($feature_id,$change_value,$field)
	{
		$sql = "UPDATE feature  SET $field ='$change_value' WHERE feature_id = $feature_id ";
		
		return $this->feature_m->change_info($sql);
	}
	/**
	* 修改feature 
	*
	* @param   int           $feature_id             特点主键
	* @param   string        $feature_title          特点标题
	* @param   string        $feature_pic_min        特点小图
	* @param   string        $feature_pic_max        特点大图
	* @param   string        $feature_intro          特点描述
	* @return  int
	*/
	function update_feature($feature_id ,$feature_title ,$feature_pic_min  ,$feature_pic_max  ,$feature_intro )
	{
		$sql = "UPDATE feature  SET feature_title ='$feature_title',feature_pic_min = '$feature_pic_min',
		feature_pic_max = '$feature_pic_max' ,feature_intro = '$feature_intro' , update_time = '".$_SERVER['REQUEST_TIME']."',
		update_user = '".$_SESSION['AName']."'	WHERE feature_id = $feature_id ";
		
		return $this->feature_m->change_info($sql);
	}

}
?>